package com.csd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.csd.dao.BaseDao;
import com.csd.model.CarType;
import com.csd.model.OtherPlase;
import com.csd.util.HttpHelper;

/**
 * Servlet implementation class CarTypeServlet
 */
@WebServlet("/CarTypeServlet")
public class CarTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CarTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String beginTime = request.getParameter("beginTime");
		String endTime = request.getParameter("endTime");
		String cityID = request.getParameter("cityID");
		String tplaceid = request.getParameter("tplaceid");
		String cityName = request.getParameter("cityName");
		JSONObject obj = new JSONObject();
		obj.put("beginTime", beginTime);
		obj.put("endTime", endTime);
		obj.put("cityID", cityID);
		obj.put("tplaceid", tplaceid);
		obj.put("orderby", 2);
		String objJson = HttpHelper.HttpPost(obj.toString(), BaseDao.CARS);
		JSONObject carObj = JSONObject.fromObject(objJson.toString());
		String msg = carObj.getString("msg");
		int status = carObj.getInt("status");
		String data = carObj.getString("data");
		if(status == 0){
			JSONObject dataObj = JSONObject.fromObject(data);
			String carTypeJson = dataObj.getString("data");
			JSONArray carArray = JSONArray.fromObject(carTypeJson);
			List<CarType> carList = new ArrayList<CarType>();
 			for (int i = 0; i < carArray.size(); i++) {
				JSONObject carJson = JSONObject.fromObject(carArray.get(i));
				String cartypename = carJson.getString("cartypename");
				String gearsname = carJson.getString("gearsname");
				String wingnumname =  carJson.getString("wingnumname");
				String seatname = carJson.getString("seatname");
				String isSpecialsnm = carJson.getString("IsSpecialsnm");
				String img = carJson.getString("img");
				String dates = carJson.getString("dates");
				String average = carJson.getString("amt");
				String iscan = carJson.getString("iscan");
				String amt = carJson.getString("amt");
				String isSpecials = carJson.getString("IsSpecials");
				CarType carType = new CarType();
				carType.setCartypename(cartypename);
				carType.setGearsname(gearsname);
				carType.setWingnumname(wingnumname);
				carType.setSeatname(seatname);
				carType.setIsSpecialsnm(isSpecialsnm);
				carType.setImg(img);
				carType.setDates(dates);
				carType.setAverage(average);
				carType.setAmt(amt);
				carType.setIscan(iscan);
				if(iscan.equals("False")){
					int otherplacescnt = carJson.getInt("otherplacescnt");
					String otherplaces = carJson.getString("otherplaces");
							JSONArray array = JSONArray.fromObject(otherplaces);
					List<OtherPlase> list = new ArrayList<OtherPlase>();
					for (int j = 0; j < array.size(); j++) {
						JSONObject otherJson = JSONObject.fromObject(array.get(j));
						String tradareaPlaceName = otherJson.getString("TradareaPlaceName");
						String cartypeid = otherJson.getString("cartypeid");
						String address = otherJson.getString("Address");
						String sTime = otherJson.getString("sTime");
						String eTime = otherJson.getString("eTime");
						String phone = otherJson.getString("Phone");
						String longitude = otherJson.getString("longitude");
						String latitude = otherJson.getString("latitude");
						String traffice = otherJson.getString("Traffice");
						String placeID = otherJson.getString("PlaceID");
						OtherPlase other = new OtherPlase();
						other.setTradareaPlaceName(tradareaPlaceName);
						other.setCartypeid(cartypeid);
						other.setAddress(address);
						other.setsTime(sTime);
						other.seteTime(eTime);
						other.setPhone(phone);
						other.setLongitude(longitude);
						other.setLatitude(latitude);
						other.setTraffice(traffice);
						other.setPlaceId(placeID);
						list.add(other);
					}
					carType.setOtherplacescnt(otherplacescnt);
					carType.setOtherplaces(list);
					}
				carList.add(carType);
				}
 			request.getSession().setAttribute("carList", carList);
 			request.getSession().setAttribute("cityName", cityName);
 			request.getSession().setAttribute("beginTime", beginTime);
 			request.getSession().setAttribute("endTime", endTime);
 			response.sendRedirect("www/CarTypeList.jsp");
		}else{
			PrintWriter out = response.getWriter(); 
			out.println("<script language=\"javaScript\">");
			out.println("alert(" + msg + ");");
			out.println(" window.location='www/CityTime.jsp'");
			out.println("</script>");
		}
	}

}
